package com.luyuan.crm.sms.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.Order;

import com.luyuan.crm.sms.model.SmsLogModel;
import com.luyuan.crm.sms.service.SmsLogService;
import com.ruijie.foundation.dao.extend.support.HibernateEntityExtendDao;
import com.ruijie.foundation.query.Page;
import com.ruijie.foundation.query.criteria.CriteriaQueryTemplate;
import com.ruijie.foundation.query.criteria.CriteriaTemplate;

@SuppressWarnings("unchecked")
public class SmsLogServiceImpl extends HibernateEntityExtendDao<SmsLogModel> implements SmsLogService {
    private static final Log logger = LogFactory.getLog(SmsLogServiceImpl.class);

    @Override
    public void recordLog(SmsLogModel entity) {
        this.save(entity);
    }

    @Override
    public Page pagedQuery(CriteriaTemplate criteria, int pageNo, int pageSize, List<Order> orders) {
        CriteriaQueryTemplate qt = new CriteriaQueryTemplate(getSessionFactory());
        return qt.pagedQuery(criteria, pageNo, pageSize, orders);
    }

    @Override
    public void clearLog() {
        Calendar ca = Calendar.getInstance();// 得到一个Calendar的实例
        ca.setTime(new Date()); // 设置时间为当前时间
        ca.add(Calendar.YEAR, -1); // 年份减1
        Date lastMonth = ca.getTime(); // 结果
        logger.info("clean sms log before: " + lastMonth);
        update("delete SmsLogModel where sendDate < ?", lastMonth);
    }
}
